package viper

import (
	"basic/log"
	"fmt"
	"github.com/fsnotify/fsnotify"
	flag "github.com/spf13/pflag"
	"github.com/spf13/viper"
)

type Server struct {
	Port int
}

var GVA_CONFIG Server

// 读取配置文件
func Viper() *viper.Viper {
	var config string
	flag.StringVar(&config, "c", "config.yaml", "choose config file.")
	flag.Parse()
	v := viper.New()
	v.SetConfigFile(config)
	err := v.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("Fatal error config file: %s \n", err))
	}
	v.WatchConfig()

	v.OnConfigChange(func(e fsnotify.Event) {
		log.SugarLogger.Infof("config file changed:%s", e.Name)
		if err := v.Unmarshal(&GVA_CONFIG); err != nil {
			log.SugarLogger.Errorf("加载配置文件错误%s", err)
		}
	})

	if err := v.Unmarshal(&GVA_CONFIG); err != nil {
		log.SugarLogger.Infof("读取配置文件出错%s", err)
	}
	return v

}
